{extend name="admin@publics/base"/}
{block name="title"}行为管理{/block}
{block name="body"}
<script>
//页面变量
_PAGE['adminAction'] = {
	_ID:0
};
/*==========控制器==========*/
Ext.define('admin.action.CURDViewController', {
	extend: 'Tg.tx.CURDViewController',
	alias: 'controller._adminActionController',
    //规则管理
	_ColumnPlug:function(_Grid, _Rindex, _Cindex) {
		var _Me	= this;
		var _V	= _Me.getView();
		var _VM	= _V.getViewModel();
		var _Pk = _VM.get('_Pk');
        var _Select = _Grid.getStore().getAt(_Rindex);
		if(_Select.get('id') == ''){
			Ext.Msg.alert('错误', _Select.get('title')+' 主键为空');
			return false;
		}
		_PAGE['adminAction']['_ID'] = _Select.get('id');
        var _Win = Ext.create('Tg.tx.Window',{
			width:560,
			height:540,
			layout:'fit',
			title: _Select.get('title')+' [ '+_Select.get('name')+' ] 行为插件管理',
			items:{xtype:'admin_ActionPlug_index'}
        });
		_Win.show();
	},
});
/*==========模型==========*/
Ext.define('admin.action.CURDViewModel', {
	extend: 'Tg.tx.CURDViewModel',
	alias: 'viewmodel._adminActionViewModel',
    //Data
    data:{
    	//权限
        _Auth:{
        	index	: {if condition="_CheckAuth('admin/Action/index')"}true{else/}false{/if},
        	add		: {if condition="_CheckAuth('admin/Action/add')"}true{else/}false{/if},
        	edit	: {if condition="_CheckAuth('admin/Action/edit')"}true{else/}false{/if},
        	del		: {if condition="_CheckAuth('admin/Action/del')"}true{else/}false{/if},
        	plug	: {if condition="_CheckAuth('admin/ActionPlug/index')"}true{else/}false{/if},
        },
        _Url:{
        	index	: _PATH['site']+'/admin/action/index',
        	add		: _PATH['site']+'/admin/action/add',
        	edit	: _PATH['site']+'/admin/action/edit',
        	del		: _PATH['site']+'/admin/action/del',
        },
        //操作视图
        _Ext:{
        	index	: 'admin.action.index',
        	serch	: 'admin.action.serch',
        	add		: 'admin.action.updata',
        	edit	: 'admin.action.updata',
        	del		: 'admin.action.del',
        },
		_Pk:'id',
    },
    //Store
	stores: {
		_MainStore: {
			sorters: [{property: 'id',direction: 'desc'}],
			pageSize: 20,
			remoteSort: true,
			remoteFilter:true,
			proxy: {
				type: 'ajax',
				api: {read:_PATH['site']+'/admin/action/index'},
				actionMethods:{read: "POST"},
				reader: {type: 'json',rootProperty: 'rows',totalProperty: 'total'}
			},
		}
	},
});
/*==========视图==========*/
//列表
Ext.define('admin.action.index', {
	extend: 'Tg.tx.CURDList',
	alias: 'widget.admin_Action_index',
	requires: [
		'admin.action.CURDViewController',
		'admin.action.CURDViewModel',
	],
	controller: '_adminActionController',
	viewModel: '_adminActionViewModel',
    _TbarLBefore:[
        {xtype: 'label',text: '行为管理',itemId:'_Tit',style: 'font-size:16px; padding-right:10px;margin-right: 10px;border-right: dotted 1px #ccc;'},
    ],
    items:[
		{
			xtype:'CURDListGrid',
			bind:{store:'{_MainStore}'},
			columns:[
        		{xtype:"actioncolumn",header:"插件",width: 50,iconCls:'tf tf-plug',align:'center',locked: true,handler: '_ColumnPlug',bind:{hidden:'{!_Auth.plug}'}},
        		{xtype:"actioncolumn",header:"更新",width: 50,iconCls:'tf tf-edit',align:'center',locked: true,handler: '_ColumnEdit',bind:{hidden:'{!_Auth.edit}'}},
				{xtype:"actioncolumn",header:"删除",width: 50,iconCls:'tf tf-del',align:'center',locked: true,handler: '_ColumnDel',bind:{hidden:'{!_Auth.del}'}},
				{dataIndex:'id', header :'主键', width :60, hidden:false, locked:true, sortable:true },
				{dataIndex:'name', header :'标识', width :120, hidden:false, locked:false, sortable:false },
				{dataIndex:'title', header :'标题', width :120, hidden:false, locked:false, sortable:false },
				{dataIndex:'identifying', header :'标志', width :220, hidden:false, locked:false, sortable:true, renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {return _Res_Val(_ExtendList,'extend', value);}},
				{dataIndex:'status', header :'状态', width :50, hidden:false, locked:false, sortable:true, renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {return _Res_Val(_Dictionary,'sys_status', value);}},
				{dataIndex:'create_time', header :'创建时间', width :110, hidden:true, locked:false, sortable:true },
				{dataIndex:'update_time', header :'更新时间', width :110, hidden:true, locked:false, sortable:true },
			],
		}
	],
});

//搜索
Ext.define('admin.action.serch', {
	extend: 'Tg.tx.Window',
	alias: 'widget.admin_Action_serch',
	requires: [
		'admin.action.CURDViewController',
		'admin.action.CURDViewModel',
	],
	controller: '_adminActionController',
	viewModel: '_adminActionViewModel',
    title:'搜索',
	width:600,
	height:460,
    items:{
    	xtype:'CURDForm',
        items:[ 
			{items: [
				{fieldLabel: '标识', name: 's_name',xtype: "textfield", columnWidth: 0.5},
				{fieldLabel: '标题', name: 's_title',xtype: "textfield", columnWidth: 0.5},
			]},
			{items: [
				{fieldLabel: '标志', name: 's_identifying',xtype: "combobox",displayField: "text",valueField: "value",store: {fields: ["text", "value"],data:_ExtendList['extend']}},
			]},
			{items: [
				{fieldLabel: '日志规则', name: 's_log',xtype: "textfield"},
			]},
			{items: [
				{fieldLabel: '状态', name: 's_status',xtype: "combobox",displayField: "text",valueField: "value",store: {fields: ["text", "value"],data:_Dictionary['sys_status']}, columnWidth: 0.5},
				{fieldLabel: '主键', name: 's_id',xtype: "textfield", columnWidth: 0.5},
			]},
			{items: [
				{fieldLabel: '描述', name: 's_remark',xtype: "textfield"},
			]},
			{items: [
				{fieldLabel: '起始创建时间', name: 's_create_time_min',xtype: "datetimefield",columnWidth: 0.5},
				{fieldLabel: '截止创建时间', name: 's_create_time_max',xtype: "datetimefield",columnWidth: 0.5},
			]},
			{items: [
				{fieldLabel: '起始更新时间', name: 's_update_time_min',xtype: "datetimefield",columnWidth: 0.5},
				{fieldLabel: '截止更新时间', name: 's_update_time_max',xtype: "datetimefield",columnWidth: 0.5},
			]},
        ]
    }
});



//操作 
Ext.define('admin.action.updata', {
	extend: 'Tg.tx.Window',
	alias: 'widget.admin_Action_updata',
	requires: [
		'admin.action.CURDViewController',
		'admin.action.CURDViewModel',
	],
	controller: '_adminActionController',
	viewModel: '_adminActionViewModel',
    title:'行为 - 操作',
    items:{
    	xtype:'CURDForm',
        items:[
			{items: [
				{xtype:'textfield', fieldLabel: '标识', name: 'name', itemId: 'name', cls: '', columnWidth: 0.5, height: 30, readOnly: false, emptyText:'请输入...'},
				{xtype:'textfield', fieldLabel: '标题', name: 'title', itemId: 'title', cls: '', columnWidth: 0.5, height: 30, readOnly: false, emptyText:'请输入...'},
			]},
			{items: [
				{xtype:'combobox', fieldLabel: '标志', name: 'identifying', itemId: 'identifying', cls: '', columnWidth: 1, height: 30, readOnly: false, emptyText:'请选择', editable:false, displayField:'text', valueField: 'value', store:{fields:['text','value'],data:_ExtendList['extend']}},
			]},
			{items: [
				{xtype:'textarea', fieldLabel: '日志规则', name: 'log', itemId: 'log', cls: '', columnWidth: 1, height: 30, readOnly: false, preventScrollbars:true, emptyText:'请输入...'},
			]},
			{items: [
				{xtype:'combobox', fieldLabel: '状态', name: 'status', itemId: 'status', cls: '', columnWidth: .5, height: 30, readOnly: false, emptyText:'请选择', editable:false, displayField:'text', valueField: 'value', store:{fields:['text','value'],data:_Dictionary['sys_status']}},
			]},
			{items: [
				{xtype:'textarea', fieldLabel: '描述', name: 'remark', itemId: 'remark', cls: '', columnWidth: 1, height: 80, readOnly: false, preventScrollbars:true, emptyText:'请输入...'},
			]},
			{xtype:'hiddenfield', fieldLabel: '主键', name: 'id', itemId: 'id', cls: '', columnWidth: 1, height: 30, readOnly: false},
        ]
    }
});

//实例化
Ext.application({
	name: 'admin',
	extend: 'Ext.app.Application',
	autoCreateViewport: 'admin.action.index'
});


/*==========控制器==========*/
Ext.define('admin.action_plug.CURDViewController', {
	extend: 'Tg.tx.CURDViewController',
	alias: 'controller._adminActionPlugController',
	//新增
	_Add:function(button, e, eOpts){
		var _Me = this;
		if(!_Me._CheckAuth('add')){return false;}
		var _VM = _Me.getView().getViewModel();
		var _Auth = _VM.get('_Auth.add');
		var _Store = _VM.get('_MainStore');
		var _Url = _VM.get('_Url.add')+'/action_id/'+_PAGE['adminAction']['_ID'];
		var _Ext = _VM.get('_Ext.add');
        var _Win = Ext.create(_Ext,{
            bbar: new Ext.Toolbar({height: 45,style: 'background: #5fa2dd;',items: [	
                '->',
                {
                    text:'保存',
                    handler:function(_SubBtn){
                        var _Form	= _SubBtn.up("window").down("form").getForm();
                        var _Params	= _Form.getValues();
						var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store});
                        if(_Return){
                        	_SubBtn.up("window").close();
                        }
                        
                    }
                },
                {
                    text:'取消',
                    handler:function(_CloseBtn){
                        _CloseBtn.up("window").close();
                    }
                }
            ]})
        });
		Ext.create('Tg.tx.Action').getDataToForm({form:_Win.down("form").getForm(),url:_Url})
	},
	
	//行_配置
    _ColumSetting:function(_Grid, _Rindex, _Cindex) {
		var _Me	= this;
		var _V	= _Me.getView();
		var _VM	= _V.getViewModel();
		var _Store = _VM.get('_MainStore');
		var _Url = _VM.get('_Url.setting');
		var _Ext = _VM.get('_Ext.setting');
		var _Pk = _VM.get('_Pk');
        var _Select = _Grid.getStore().getAt(_Rindex);
		var _FormFiel = '';
		//获取 配置表单数据
		Ext.Ajax.request({
			url: _Url,
			method: 'get',
			params: {'id': _Select.get('name'),'ext':'js'},
			async: false, 
			success: function (response, opts) {
				_Rec = _TObj(response.responseText);
				if (parseInt(_Rec.code) == 200) {
					_FormField =  eval('(' + _Rec.data + ')');
				} else {
					Ext.Msg.alert('错误', _Rec.msg);
					return false;
				}
			},
			failure: function (response, opts) {
				Ext.MessageBox.alert('错误', '服务器异常');
				return false;
			}
		});
        var _Win = Ext.create('Tg.tx.Window',{
			requires: [
				'admin.action_plug.CURDViewController',
				'admin.action_plug.CURDViewModel',
			],
			controller: '_adminActionPlugController',
			viewModel: '_adminActionPlugViewModel',
            title:'配置插件',
            items:{
                xtype:'CURDForm',
                items:_FormField
            },
			width:600,
			height:500,
            bbar: new Ext.Toolbar({height: 45,style: 'background: #5fa2dd;',items: [	
                '->',
                {
                    text:'保存',
                    handler:function(_SubBtn){
                        var _Form	= _SubBtn.up("window").down("form").getForm();
                        var _Params	= _Form.getValues();
						var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store});
                        if(_Return){
                        	_SubBtn.up("window").close();
                        }
                        
                    }
                },
                {
                    text:'取消',
                    handler:function(_CloseBtn){
                        _CloseBtn.up("window").close();
                    }
                }
            ]})
        });
		var _Params={};
		_Params[_Pk]=_Select.get(_Pk);
        Ext.Ajax.request({
            url: _Url,
            method: 'get',
            params:_Params,
            async: false,
            success: function (response, options) {
                var _Rec = _TObj(response.responseText);
                if (parseInt(_Rec.code) == 200) {
                	_Win.down("form").getForm().setValues(_Rec.data);
                } else {
                    Ext.Msg.alert('错误', _Rec.msg);

                }
            },
            failure: function () {
            }
        });
    },
});
/*==========模型==========*/
Ext.define('admin.action_plug.CURDViewModel', {
	extend: 'Tg.tx.CURDViewModel',
	alias: 'viewmodel._adminActionPlugViewModel',
    //Data
    data:{
    	//权限
        _Auth:{
        	index	: {if condition="_CheckAuth('admin/ActionPlug/index')"}true{else/}false{/if},
			serch	: false ,
        	add		: {if condition="_CheckAuth('admin/ActionPlug/add')"}true{else/}false{/if},
        	edit	: {if condition="_CheckAuth('admin/ActionPlug/edit')"}true{else/}false{/if},
        	del		: {if condition="_CheckAuth('admin/ActionPlug/del')"}true{else/}false{/if},
        	setting	: {if condition="_CheckAuth('admin/ActionPlug/setting')"}true{else/}false{/if},
        },
        _Url:{
        	index	: _PATH['site']+'/admin/action_plug/index',
        	add		: _PATH['site']+'/admin/action_plug/add',
        	edit	: _PATH['site']+'/admin/action_plug/edit',
        	del		: _PATH['site']+'/admin/action_plug/del',
        	setting	: _PATH['site']+'/admin/action_plug/setting',
        },
        //操作视图
        _Ext:{
        	index	: 'admin.action_plug.index',
        	serch	: 'admin.action_plug.serch',
        	add		: 'admin.action_plug.updata',
        	edit	: 'admin.action_plug.updata',
        	del		: 'admin.action_plug.del',
        	setting	: 'admin.action_plug.setting',
        },
		_Pk:'id',
    },
    //Store
	stores: {
		_MainStore: {
			sorters: [{property: 'id',direction: 'desc'}],
			pageSize: 20,
			remoteSort: true,
			remoteFilter:true,
			proxy: {
				type: 'ajax',
				api: {read:_PATH['site']+'/admin/action_plug/index'},
				actionMethods:{read: "POST"},
				reader: {type: 'json',rootProperty: 'rows',totalProperty: 'total'}
			},
		}
	},
});
/*==========视图==========*/
//列表
Ext.define('admin.action_plug.index', {
	extend: 'Tg.tx.CURDList',
	alias: 'widget.admin_ActionPlug_index',
	requires: [
		'admin.action_plug.CURDViewController',
		'admin.action_plug.CURDViewModel',
	],
	controller: '_adminActionPlugController',
	viewModel: '_adminActionPlugViewModel',
	initComponent:function(){
        var _Me = this;
        var _VM = _Me.getViewModel();
		_VM.set('_MainStore.proxy.api.read',_PATH['site']+'/admin/action_plug/index/action_id/'+_PAGE['adminAction']['_ID']);
        _Me.callParent(arguments);
	},
    items:[
		{
			xtype:'CURDListGrid',
			bind:{store:'{_MainStore}'},
			columns:[
        		{xtype:"actioncolumn",header:"配置",width: 50,align:'center',locked: true,handler: '_ColumSetting',bind:{hidden:'{!_Auth.setting}'},getClass : function(v, metadata, record){
					if(!(record.data.status != -1) || record.data.is_conf != 1){
						return 'x-hidden';
					}else{
						return 'tf tf-setting';
					}
				}},
        		{xtype:"actioncolumn",header:"更新",width: 50,iconCls:'tf tf-edit',align:'center',locked: true,handler: '_ColumnEdit',bind:{hidden:'{!_Auth.edit}'}},
				{xtype:"actioncolumn",header:"删除",width: 50,iconCls:'tf tf-del',align:'center',locked: true,handler: '_ColumnDel',bind:{hidden:'{!_Auth.del}'}},
				{dataIndex:'id', header :'主键', width :60, hidden:true, locked:true, sortable:true },
				{dataIndex:'plug_id', header :'插件', width :220, hidden:false, locked:false, sortable:true, renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {return _Res_Val(_ExtendList,'plug', value);}},
				{dataIndex:'func', header :'方法', width :120, hidden:false, locked:false, sortable:false },
				{dataIndex:'status', header :'状态', width :50, hidden:false, locked:false, sortable:true, renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {return _Res_Val(_Dictionary,'sys_status', value);}},
				{dataIndex:'create_time', header :'创建时间', width :110, hidden:true, locked:false, sortable:true },
				{dataIndex:'update_time', header :'更新时间', width :110, hidden:true, locked:false, sortable:true },
			],
		}
	],
});


//操作 
Ext.define('admin.action_plug.updata', {
	extend: 'Tg.tx.Window',
	alias: 'widget.admin_ActionPlug_updata',
	requires: [
		'admin.action_plug.CURDViewController',
		'admin.action_plug.CURDViewModel',
	],
	controller: '_adminActionPlugController',
	viewModel: '_adminActionPlugViewModel',
    title:'行为挂载插件 - 操作',
	width:500,
	height:240,
    items:{
    	xtype:'CURDForm',
        items:[
			{items: [
				{xtype:'combobox', fieldLabel: '插件', name: 'plug_id', itemId: 'plug_id', cls: '',  height: 30, readOnly: false, emptyText:'请选择', editable:false,valueField : "value",displayField : "text",store:{fields:['value','text'],data:_ExtendList['plug']},listeners:{
					'change': function(obj, newValue, oldValue, eOpts ){
						var _Me = this;+''
						var funcObj = _Me.up('form').down('#func')
						funcObj.store.proxy.url = _PATH['site']+'/admin/api/plug_func_list/plug_id/'+newValue+'?verify_code={:_AutoCodeUrl('admin/Action/index')}';
						funcObj.store.reload();
					}
				}},
			]},
			{items: [
				{xtype:'combobox', fieldLabel: '方法', name: 'func', itemId: 'func', cls: '', height: 30, readOnly: false, emptyText:'请选择', editable:false,valueField : "value",displayField : "text",store:{autoLoad: true,fields:['value','text'],proxy:{type: 'ajax',url:_PATH['site']+'/admin/api/plug_func_list?verify_code={:_AutoCodeUrl('admin/Action/index')}'}}},
			]},
			{items: [
				{xtype:'combobox', fieldLabel: '状态', name: 'status', itemId: 'status', cls: '', height: 30, readOnly: false, emptyText:'请选择', editable:false, displayField:'text', valueField: 'value', store:{fields:['text','value'],data:_Dictionary['sys_status']}},
			]},
			{xtype:'hiddenfield', fieldLabel: '主键', name: 'id', itemId: 'id', cls: '', columnWidth: 1, height: 30, readOnly: false},
			{xtype:'hiddenfield', fieldLabel: '行为', name: 'action_id', itemId: 'action_id', cls: '', columnWidth: 1, height: 30, readOnly: false},
        ]
    }
});
</script>
{/block}
